﻿Imports System.Xml.Serialization

Module Utils
    ''' <summary>
    ''' Returns the date of the specified day of the week.
    ''' </summary>
    ''' <param name="day">The day of the week you want returned.</param>
    ''' <param name="CurrentDate">The date to start from.</param>
    ''' <returns></returns>
    ''' <remarks>If you pass 3/2/2010 as the date and Monday as the day you want.  It will return 3/8/2010.</remarks>
    Function GetNextDay(ByVal day As DayOfWeek, ByVal CurrentDate As Date) As Date
        While CurrentDate.DayOfWeek <> day
            CurrentDate = CurrentDate.AddDays(1)

        End While
        Return CurrentDate
    End Function
    Sub debugLogWithStack(logger As NLog.Logger, message As String)
        Dim stk As New StackTrace(1)
        logger.Debug(Function()
                         Return message & vbCrLf & stk.ToString
                     End Function)
    End Sub
    Sub LaunchProjectWebsite()
        If (My.Settings.ProjectWebsite & "").ToLower.StartsWith("http://") Then
            Dim tf As New Threading.Tasks.TaskFactory()
            tf.StartNew(Sub()
                            Try
                                Process.Start(My.Settings.ProjectWebsite)
                            Catch ex As Exception

                            End Try

                        End Sub)

        End If
    End Sub
    ''' <summary>
    ''' Returns the date of a specified day of the week.
    ''' </summary>
    ''' <param name="day">Which day of the week do you want returned.</param>
    ''' <param name="CurrentDate">The date to start from.</param>
    ''' <returns></returns>
    ''' <remarks>If you pass 3/2/2010 as the date and Monday as the day you want.  It will return 3/1/2010.</remarks>
    Function GetLastDay(ByVal day As DayOfWeek, ByVal CurrentDate As Date) As Date
        While CurrentDate.DayOfWeek <> day
            CurrentDate = CurrentDate.AddDays(-1)

        End While
        Return CurrentDate
    End Function

    Function GetDayTimeSpanFromDateRange(ByVal dtStart As Date, ByVal dtEnd As Date) As TimeSpan
        Dim dt As New Date(dtEnd.Year, dtEnd.Month, dtEnd.Day, dtStart.Hour, dtStart.Minute, dtStart.Second)

        Return dtEnd.Subtract(dt)


    End Function

    Function CheckNewRelease(ByRef URL As Uri) As Boolean
        If Not My.Computer.Network.IsAvailable Then Return False

        Dim latest As BlogPost
        Try
            latest = (From post In RssHelper.RetrieveFeeds(My.Settings.UpdateFeedURL)).FirstOrDefault
            Dim st, en As Integer
            st = latest.Title.IndexOf("[") + 1
            en = latest.Title.IndexOf("]")
            Dim v As New Version(latest.Title.Substring(st, en - st))
            URL = latest.Url
            Return v > My.Application.Info.Version

        Catch ex As Exception
            Return False
        End Try







    End Function


End Module
<XmlRoot("Dictionary", Namespace:="")>
<Serializable()>
Public Class SerializableDictionary(Of tkey, tValue)
    Inherits Dictionary(Of tkey, tValue)
    Implements IXmlSerializable

    'todo implement http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx

    Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
        Return Nothing
    End Function

    Public Overridable Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
        Dim ks As New XmlSerializer(GetType(tkey))
        Dim vs As New XmlSerializer(GetType(tValue))
        Dim isEmpty As Boolean = reader.IsEmptyElement
        reader.Read()
        If isEmpty Then Return

        While reader.NodeType <> Xml.XmlNodeType.EndElement

            reader.ReadStartElement("item")

            reader.ReadStartElement("key")
            Dim k As tkey = ks.Deserialize(reader)
            reader.ReadEndElement()

            reader.ReadStartElement("value")
            Dim v As tValue = vs.Deserialize(reader)
            reader.ReadEndElement()
            Me.Add(k, v)

            reader.ReadEndElement()
            reader.MoveToContent()

        End While
        reader.ReadEndElement()
    End Sub

    Public Overridable Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
        Dim ks As New XmlSerializer(GetType(tkey))
        Dim vs As New XmlSerializer(GetType(tValue))

        For Each k In Me.Keys
            writer.WriteStartElement("item")

            writer.WriteStartElement("key")
            ks.Serialize(writer, k)
            writer.WriteEndElement()

            writer.WriteStartElement("value")
            Dim v As tValue = Me(k)
            vs.Serialize(writer, v)
            writer.WriteEndElement()

            writer.WriteEndElement()
        Next
    End Sub
End Class